package com.xyz.tasks.bf;

import com.free.common.vo.ResultModel;
import com.free.ext.logger.FreeLogger;
import com.free.ext.logger.FreeLoggerFactory;
import com.xyz.domain.constants.EhCacheConstants;
import com.xyz.domain.table.SLottery;
import com.xyz.domain.table.SLotteryPeriod;
import com.xyz.domain.table.SLotterydetail;
import com.xyz.lottery.LotteryProbablity;
import com.xyz.lottery.Lotteryer;
import com.xyz.service.LotteryInfoService;
import com.xyz.service.LotteryPeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LotteryCalcuateBf {

    private static final FreeLogger LOG = FreeLoggerFactory.getLogger(LotteryCalcuateBf.class);

    @Autowired
    private LotteryPeriodService lotteryPeriodService;

    @Autowired
    private LotteryProbalityBf lotteryProbalityBf;

    @Autowired
    private LotteryInfoService lotteryInfoService;

    public ResultModel getLotteryRes(String lotteryCode){

       LotteryProbablity lotteryRes = getLuckNum(lotteryCode);
       LOG.info("抽奖CODE:{},结果:{}", lotteryCode, lotteryRes);
       SLottery lotteryInfo = lotteryInfoService.getLotteryInfo(lotteryCode);

       SLotterydetail lotterydetail = getLotteryDetail(lotteryInfo, lotteryRes);
       SLotteryPeriod periodInfo = lotteryPeriodService.getCurrentLotteryPeriodInfo(lotteryCode);

       // 期数和奖励


       return ResultModel.success();
    }



    public LotteryProbablity getLuckNum(String lotteryCode){
        Lotteryer lotteryer = lotteryProbalityBf.getLotteryerByCode(lotteryCode);
        return lotteryer.lottery();
    }

    private SLotterydetail getLotteryDetail(SLottery lotteryInfo, LotteryProbablity lotteryRes){
        SLotterydetail lotterydetail = lotteryInfo.getDetails().stream().filter(x->{
          return x.getId() == lotteryRes.getIdx();
        }).findFirst().orElse(null);
        return lotterydetail;
    }

}
